from django import forms
from mytest.models import UserInfo
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
class RegisterModelForm(forms.ModelForm):
    mobile_phone=forms.CharField(label='手机号',validators=[RegexValidator(r'^1[3-9]\d{9}$','手机号格式错误'),])
    password=forms.CharField(widget=forms.PasswordInput(),label='密码')
    confirm_password=forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-control'}),label='重复密码')
    code=forms.CharField(label='验证码')
    class Meta:
        model=UserInfo
        # fields="__all__"
        fields=['username','email','password','confirm_password','mobile_phone','code']
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)

        for item,field in self.fields.items():
            field.widget.attrs['class']='form-control'
            field.widget.attrs['placeholder']=f'请输入{field.label}'